package com.cjb.cloudsystem.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.SysMsg;
import com.cjb.cloudsystem.mapper.SysMsgMapper;
import com.cjb.cloudsystem.service.SysMsgService;
import com.cjb.cloudsystem.vo.SearchMsgVO;
import com.cjb.cloudsystem.vo.SysMsgVO;
import com.cjb.common.exception.ServerException;
import com.cjb.common.mybatisUtil.impl.BaseRepositoryImpl;
import com.cjb.common.util.Common;
import com.cjb.eighteenart.util.MsgAPIUtil;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.ls.LSInput;

import java.util.List;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class SysMsgsServiceImpl
 * @Date 2023-07-27 21:41
 */
@Service
@AllArgsConstructor
public class SysMsgServiceImpl extends BaseRepositoryImpl<SysMsgMapper, SysMsg> implements SysMsgService {
    /**
     * 日志
     */
    private final static Logger log = LoggerFactory.getLogger(SysMsgServiceImpl.class);
    /*
     * 注入mapper层
     */
    private final SysMsgMapper sysMsgMapper;
    /**
     * 注入消息服务
     */
    @Autowired
    private MsgAPIUtil msgAPIUtil;

    /**
     * 单体保存
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public boolean save(SysMsg sysMsg) {
        if (Common.isEmpty(sysMsg)) {
            throw new ServerException("sysObject is empty");
        }
        baseMapper.insert(sysMsg);
        return true;
    }

    /**
     * 批量保存
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Integer listSave(List<SysMsg> sysMsg) {
        return baseMapper.insertBatchSomeColumn(sysMsg);
    }

    /**
     * 批量更新
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void listUpdate(List<SysMsg> sysMsg) {
        saveOrUpdateBatch(sysMsg, sysMsg.size());
    }

    /**
     * 批量删除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void listDelete(List<SysMsg> sysMsg) {
//        List<Long> collect = sysMsg.stream().map(SysMsg::getMsgId)
//        .distinct()
//        .collect(Collectors.toList());
//        sysMsgMapper.deleteBatchIds(collect);
    }

    /**
     * 通过id删除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public int deleteById(SysMsg sysMsg) {
        return sysMsgMapper.deleteById(sysMsg.getMsgId());
    }

    /**
     * 单个查询
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public SysMsg queryById(SysMsg sysMsg) {
        return sysMsgMapper.selectById(sysMsg.getMsgId());
    }

    /**
     * 分页
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Page queryPage(Page sysMsgPage, SysMsgVO sysMsgVO) {
        return sysMsgMapper.selectPage(sysMsgPage, getWrapper(sysMsgVO));
    }

    /**
     * 查询属性
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    private LambdaQueryWrapper<SysMsg> getWrapper(SysMsgVO query) {
        LambdaQueryWrapper<SysMsg> wrapper = Wrappers.lambdaQuery();
        if (Common.isNotNull(query.getMsgId())) {
            wrapper.eq(SysMsg::getMsgId, query.getMsgId());
        }
        if (Common.isNotNull(query.getUserId())) {
            wrapper.eq(SysMsg::getUserId, query.getUserId());
        }
        if (Common.isNotNull(query.getMsgContent())) {
            wrapper.eq(SysMsg::getMsgContent, query.getMsgContent());
        }
        if (Common.isNotNull(query.getMsgTheme())) {
            wrapper.eq(SysMsg::getMsgTheme, query.getMsgTheme());
        }
        if (Common.isNotNull(query.getSubRule())) {
            wrapper.eq(SysMsg::getSubRule, query.getSubRule());
        }
        if (Common.isNotNull(query.getExpectPrice())) {
            wrapper.eq(SysMsg::getExpectPrice, query.getExpectPrice());
        }
        if (Common.isNotNull(query.getMsgSendEmail())) {
            wrapper.eq(SysMsg::getMsgSendEmail, query.getMsgSendEmail());
        }
        if (Common.isNotNull(query.getMsgSendTime())) {
            wrapper.eq(SysMsg::getMsgSendTime, query.getMsgSendTime());
        }
        wrapper.eq(SysMsg::getDeleted, 0);
        wrapper.orderByDesc(SysMsg::getMsgId);
        return wrapper;
    }


    /**
     * 返回全部信息
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public List<SysMsg> returnAllData() {
        return sysMsgMapper.selectList(null);
    }
    /**
     * 查询订阅藏品
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public SearchMsgVO searchArt(String goodName) {
        String responseBody = msgAPIUtil.searchGoods(goodName, 1, 20);
        JSONObject jsonResponse = JSONObject.parseObject(responseBody);
        JSONObject data = jsonResponse.getJSONObject("data");
        JSONArray goodsArray = data.getJSONArray("data");

        SearchMsgVO searchMsgVO = new SearchMsgVO();
        for (int i = 0; i < goodsArray.size(); i++) {
            JSONObject goods = goodsArray.getJSONObject(i);
            String siteName = goods.getString("siteName");
            String name = goods.getString("goodsName");
            String price = goods.getString("price");
            // 打印或者做其他处理
            searchMsgVO.setGoodName(name);
            searchMsgVO.setSiteName(siteName);
            searchMsgVO.setPrice(price);
        }
        return searchMsgVO;
    }

    /**
     * 条件查询全部List数据
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public List<SysMsg> queryList(SysMsgVO sysMsgVO) {
        return sysMsgMapper.selectList(getWrapper(sysMsgVO));
    }

}